package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
)

// 获取设备信息
func GetDeviceInfo(p map[string]interface{}) *models.Device {
	o := orm.NewOrm()
	device := &models.Device{}
	qs := o.QueryTable("Device")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["sn"]; ok {
		qs = qs.Filter("sn", p["sn"])
	}
	if _, ok := p["username"]; ok {
		qs = qs.Filter("username", p["username"])
	}
	if _, ok := p["password"]; ok {
		qs = qs.Filter("password", p["password"])
	}
	qs.One(device)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if device.Id > 0 {
			device.TypeShow = models.DeviceType[device.Type]
			device.BindStatusShow = models.DeviceBindStatus[device.BindStatus]
			device.CreatedShow = device.Created.Format("2006-01-02 15:04:05")
			device.UpdatedShow = device.Updated.Format("2006-01-02 15:04:05")
		}
	}
	return device
}

// 添加设备
func AddDevice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	device := &models.Device{}
	sn := utils.UniqueId()
	p := map[string]interface{}{}
	p["sn"] = sn
	deviceInfo := GetDeviceInfo(p)
	for deviceInfo.Id > 0 {
		sn = utils.UniqueId()
		p["sn"] = sn
		deviceInfo = GetDeviceInfo(p)
	}
	device.Sn = sn
	school := &models.School{}
	if _, ok := input["school_id"]; ok {
		school.Id = utils.Atoi64(input["school_id"][0])
		o.Read(school)
	}
	device.School = school
	if _, ok := input["username"]; ok {
		device.Username = input["username"][0]
	}
	if _, ok := input["password"]; ok {
		device.Password = input["password"][0]
	}
	if _, ok := input["place"]; ok {
		device.Place = input["place"][0]
	}
	if _, ok := input["device_code"]; ok {
		device.DeviceCode = input["device_code"][0]
	}
	if _, ok := input["mac"]; ok {
		device.Mac = input["mac"][0]
	}
	if _, ok := input["type"]; ok {
		device.Type, _ = strconv.Atoi(input["type"][0])
	}
	return o.Insert(device)
}

// 修改设备信息
func EditDevice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("Device").Filter("id", input["id"][0]).Update(p)
}

// 获取设备列表
func GetDeviceList(p map[string]interface{}) (int64, []*models.Device) {
	var deviceList []*models.Device
	o := orm.NewOrm()
	qs := o.QueryTable("Device")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["schoolId"]; ok {
		qs = qs.Filter("school_id", p["schoolId"])
	}
	if _, ok := p["schoolName"]; ok {
		qs = qs.Filter("School__Name__contains", p["schoolName"])
	}
	if _, ok := p["type"]; ok {
		qs = qs.Filter("type", p["type"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&deviceList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, device := range deviceList {
			device.TypeShow = models.DeviceType[device.Type]
			device.BindStatusShow = models.DeviceBindStatus[device.BindStatus]
			device.CreatedShow = device.Created.Format("2006-01-02 15:04:05")
			device.UpdatedShow = device.Updated.Format("2006-01-02 15:04:05")
		}
	}
	return totalCount, deviceList
}
